package com.ftwj.demo.permission.controller;


import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.validation.constraints.Positive;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ftwj.demo.config.swagger.DataType;
import com.ftwj.demo.config.swagger.HttpMethod;
import com.ftwj.demo.config.swagger.ParamType;
import com.ftwj.demo.permission.entity.ResourceBean;
import com.ftwj.demo.permission.service.ResourceService;
import com.ftwj.demo.permission.vo.RouterEntity;
import com.ftwj.demo.global.shiro.ShiroUtil;
import com.ftwj.demo.utils.result.MessageEnum;
import com.ftwj.demo.utils.result.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
*
* <p>Title: ResourceController</p>  
* <p>Description: 描述: 权限管理前端控制器</p>
* <p>Copyright: Copyright (c) 2020 </p>  
* <p>Company: 兰州飞天网景信息产业有限公司</p>    
* @author WangSLi
* @date 2020年4月4日 下午5:42:14
* @version V1.0
 */
@RestController
@RequestMapping("/resource")
@Api(tags="资源管理接口", value="资源管理")
public class ResourceController {
	
	@Autowired
	private ResourceService service;
	
	/**
	 * <p>Title: menu</p>  
	 * <p>Description: 查询权限树</p>  
	 * @param pid
	 * @param type
	 * @param size
	 * @return 
	 * @author HeYang
	 * @date 2020年4月23日  
	 * @version 1.0
	 */
	@GetMapping("/tree")
	@ApiOperation(value="获取资源树", notes="备注",code=0,httpMethod="GET")
	@ApiImplicitParams({
		 @ApiImplicitParam(name="pid", value="父ID", dataType=DataType.INTEGER, paramType=ParamType.QUERY, defaultValue="0")
		,@ApiImplicitParam(name="type", value="类型", dataType=DataType.INTEGER, paramType=ParamType.QUERY, defaultValue="1")
		,@ApiImplicitParam(name="size", value="查询深度", dataType=DataType.INTEGER, paramType=ParamType.QUERY, defaultValue="5")
	})
	public ResultBean<?> tree(@RequestParam(defaultValue = "0") Integer pid, @RequestParam(defaultValue="1") Integer type, @RequestParam(defaultValue="5") Integer size){
		List<Map<String, Object>> list = service.getResourceTree(pid, type, size);
		return ResultBean.success(list);
	}
	
	/**
	 * <p>Title: page</p>  
	 * <p>Description: 资源分页查询</p>  
	 * @param pid
	 * @param current
	 * @param size
	 * @author HeYang
	 * @date 2020年5月6日  
	 * @version 1.0
	 */
	@GetMapping("page")
	@ApiOperation(value="资源分页查询", notes="数据",code=0,httpMethod=HttpMethod.GET)
	@ApiImplicitParams({
		 @ApiImplicitParam(name="pid", value="父ID", dataType=DataType.INTEGER, paramType=ParamType.QUERY, defaultValue="0")
		,@ApiImplicitParam(name="current", value="当前页数", dataType=DataType.INTEGER, paramType=ParamType.QUERY, defaultValue="1")
		,@ApiImplicitParam(name="size", value="查询深度", dataType=DataType.INTEGER, paramType=ParamType.QUERY, defaultValue="10")
	})
	public ResultBean<?> page(@RequestParam(required = true) Integer pid,@RequestParam(required = true) Integer current,@RequestParam(required = true) Integer size) {
		IPage<ResourceBean> page=new Page<ResourceBean>(current, size);
		ResourceBean resource = new ResourceBean();
		resource.setPid(pid);
		IPage<Map<String, Object>> data = service.page(page,resource);
		return ResultBean.success(data);
	}
	
	/**
	 * <p>Title: list</p>  
	 * <p>Description: 资源列表查询</p>  
	 * @param pid
	 * @author HeYang
	 * @date 2020年5月6日  
	 * @version 1.0
	 */
	@GetMapping("list")
	@ApiOperation(value="资源列表查询", notes="数据",code=0,httpMethod=HttpMethod.GET)
	public ResultBean<?> list(@RequestParam Integer type) {
		Wrapper<ResourceBean> queryWrapper=new QueryWrapper<ResourceBean>().eq("type", type);
		List<ResourceBean> list = service.list(queryWrapper);
		return ResultBean.success(list);
	}
	
	/**
	 * <p>Title: insert</p>  
	 * <p>Description: 资源新增</p>  
	 * @param resource
	 * @author HeYang
	 * @date 2020年4月23日  
	 * @version 1.0
	 */
	@PostMapping("/insert")
	@ApiOperation(value="资源新增", notes="数据",code=0,httpMethod=HttpMethod.POST)
	public ResultBean<?> insert(@RequestBody@Validated ResourceBean resource) {
		resource.setCreateUser(ShiroUtil.getUserId());
		resource.setCreateTime(new Date());
		resource.setState(1);
		if(service.save(resource)) {
			return ResultBean.success();
		}else {
			return ResultBean.error(MessageEnum.SERVER_FAILED);
		}
	}
	
	/**
	 * <p>Title: update</p>  
	 * <p>Description: 修改权限</p>  
	 * @param resource
	 * @author HeYang
	 * @date 2020年4月23日  
	 * @version 1.0
	 */
	@PutMapping("/update")
	@ApiOperation(value="资源修改", notes="数据",code=0,httpMethod=HttpMethod.PUT)
	public ResultBean<?> update(@RequestBody@Validated ResourceBean resource) {
		resource.setCreateUser(ShiroUtil.getUserId());
		resource.setCreateTime(new Date());
		resource.setState(1);
		if(service.updateById(resource)) {
			return ResultBean.success();
		}else {
			return ResultBean.error(MessageEnum.SERVER_FAILED);
		}
	}
	
	/**
	 * <p>Title: delete</p>  
	 * <p>Description: 删除资源</p>  
	 * @param id
	 * @author HeYang
	 * @date 2020年4月23日  
	 * @version 1.0
	 */
	@DeleteMapping("/delete/{id}")
	@ApiOperation(value="资源删除", notes="数据",code=0,httpMethod=HttpMethod.DELETE)
	@ApiImplicitParams({
		@ApiImplicitParam(name="id", value="字典编号", dataType=DataType.INTEGER, paramType=ParamType.PATH)
	})
	public ResultBean<?> delete(@Positive(message="ID必须是大于0的整数")@PathVariable Integer id) {
		if(service.removeById(id)) {
			return ResultBean.success();
		}else {
			return ResultBean.error(MessageEnum.SERVER_FAILED);
		}
	}
	
	/**
	 * <p>Title: getResouseByRoleId</p>  
	 * <p>Description: 根据角色Id获取它相关的权限</p>  
	 * @param id
	 * @author HeYang
	 * @date 2020年5月7日  
	 * @version 1.0
	 */
	@GetMapping("/role")
	@ApiOperation(value="根据角色id获取权限挂载节点", notes="备注",code=0,httpMethod="GET")
    @ApiImplicitParams({
    	@ApiImplicitParam(name="id", value="角色id", dataType=DataType.INTEGER, paramType=ParamType.QUERY),
    	@ApiImplicitParam(name="type", value="资源类型", dataType=DataType.INTEGER, paramType=ParamType.QUERY)
    })
	public ResultBean<?> getResouseByRoleId(@RequestParam(required = true) Integer id,@RequestParam(required = false) Integer type) {
		List<Integer> data = service.getResouceIdListByRoleId(id,type);
		if(data == null) {
			return ResultBean.error(MessageEnum.NULL_CONTENT);
		}
		return ResultBean.success(data.toArray());
	}
	
	@GetMapping("/router")
	@ApiOperation(value="获取当前用户路由表", notes="备注",code=0,httpMethod="GET")
	public ResultBean<?> getRouter(){
		//realm.doGetAuthorizationInfo(SecurityUtils.getSubject().getPrincipals());
		List<String> roles = ShiroUtil.getRoles();
		List<RouterEntity> data = service.getRouterByRoles(roles);
		return ResultBean.success(data);
	}
}
